package xml;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import xml.utils.XMLUtil;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileOutputStream;
import java.io.IOException;

public class PhoneInfo {
    static String src = "src/xml/PhoneInfo.xml";
    static XMLUtil util = new XMLUtil();
    public static void main(String[] args){
        try {
            Test05();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Document getDocument() throws ParserConfigurationException, IOException, SAXException {
        //创建DOM解析器的工厂实例
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        //从DOM工厂获取DOM解析器
        DocumentBuilder builder = factory.newDocumentBuilder();
        //解析XML文档，得到一个Document对象，即DOM树
        Document document = builder.parse(src);
        //返回Document对象
        return document;
    }
    //示例2
    public static void Test01() throws ParserConfigurationException, IOException, SAXException {
        Document document = getDocument();

        //得到所有Brand节点信息
        NodeList nodeList = document.getElementsByTagName("Brand");
        //循环Brand信息
        for (int i = 0;i < nodeList.getLength();i++){
            //获取第i个Brand元素信息
            Node brand = nodeList.item(i);
            //获取第i个Brand元素的属性
            Element element = (Element)brand;
            String iphoneName = element.getAttribute("name");
            //获取第i个brand元素的所有子元素的name属性
            NodeList types = element.getChildNodes();
//            System.out.println("手机："+iphoneName+types.getLength());
            for (int j = 0;j < types.getLength();j++){
                Node iphoneType = types.item(j);
                if (iphoneType.getNodeType() == Node.ELEMENT_NODE){
                    Element element1 = (Element) iphoneType;
                    String type = element1.getAttribute("name");
                    System.out.println("手机："+iphoneName+type);
                }
            }
        }
    }
    //示例3
    public static void Test02() throws ParserConfigurationException, IOException, SAXException {
        Document document = getDocument();

        //得到所有pubDate节点信息
        NodeList nodeList = document.getElementsByTagName("pubDate");
        //pubDate元素节点
        Element pubDateElement = (Element)nodeList.item(0);
        //读取文本节点
        String pubDate = pubDateElement.getFirstChild().getNodeValue();
        System.out.println(pubDate);
    }
    //示例4
    public static void Test03() throws Exception{
        Document document = getDocument();
        //创建子节点
        Element brandElement = document.createElement("Brand");
        brandElement.setAttribute("name","三星");
        //创建Type节点
        Element typeElement = document.createElement("Type");
        typeElement.setAttribute("name","NoteX");
        //添加父子关系
        brandElement.appendChild(typeElement);
        Element phoneElement = (Element)document.getElementsByTagName("PhoneInfo").item(0);
        phoneElement.appendChild(brandElement);
        //保存XML文件
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        DOMSource domSource = new DOMSource(document);
        //设置编码格式
        transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
        StreamResult result = new StreamResult(new FileOutputStream("src/xml/PhoneInfo.xml"));
        //把DOM树转换为XML文件
        transformer.transform(domSource,result);
    }
    //示例5
    public static void Test04() throws Exception{
        String src = "src/xml/PhoneInfo.xml";
        Document document = util.getDocument(src);
        NodeList brandNodes = document.getElementsByTagName("Brand");
        for (int i = 0;i < brandNodes.getLength();i++){
            Element brandElement = (Element) brandNodes.item(i);
            String brand = brandElement.getAttribute("name");
            if (brand.equals("三星")){
                brandElement.setAttribute("name","SAMSUNG");
            }
        }
        util.saveXMLFile(document,src);
    }
    //示例6
    public  static void Test05() throws Exception{
        Document document = util.getDocument(src);
        NodeList brandNodes = document.getElementsByTagName("Brand");
        for (int i = 0;i < brandNodes.getLength();i++){
            Element brandElement = (Element) brandNodes.item(i);
            String brand = brandElement.getAttribute("name");
            if (brand.equals("SAMSUNG")){
                brandElement.getParentNode().removeChild(brandElement);
            }
        }
        util.saveXMLFile(document,src);
    }
}
